package org.jeecg.modules.zcgl.tencent.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.modules.zcgl.entity.CustomerInfo;
import org.jeecg.modules.zcgl.service.ICustomerInfoService;
import org.jeecg.modules.zcgl.tencent.config.CheckLogin;
import org.jeecg.modules.zcgl.tencent.entity.CustomerAttention;
import org.jeecg.modules.zcgl.tencent.service.IAttentionInfoService;
import org.jeecg.modules.zcgl.tencent.service.ICustomerAttentionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @Description: zcgl_customer_attention
 * @Author: jeecg-boot
 * @Date:   2020-07-10
 * @Version: V1.0
 */
@Api(tags="微信客户关系信息")
@RestController
@RequestMapping("/tencent/customerAttention")
@Slf4j
public class CustomerAttentionController extends JeecgController<CustomerAttention, ICustomerAttentionService> {
	@Autowired
	private ICustomerAttentionService customerAttentionService;
	 @Autowired
	 private ICustomerInfoService customerInfoService;
	 @Autowired
	 private IAttentionInfoService attentionInfoService;

	 /**
	  * 微信用户通过客户信息绑定客户
	  * @param vo
	  * @param request
	  * @return
	  */
	 @CheckLogin
	 @ApiOperation(value="绑定信息", notes="绑定信息")
	 @PostMapping(value = "/bindingAcount")
	 public Result<?> bindingAcount(CustomerInfo vo, HttpServletRequest request) {
		 if (StringUtils.isBlank(vo.getName())){
			 return Result.error("请输入姓名/单位名称!");
		 }
		 if (StringUtils.isBlank(vo.getCode())){
			 return Result.error("请输入身份证号/信用代码!");
		 }
		 if (StringUtils.isBlank(vo.getPhone())){
			 return Result.error("请输入联系电话!");
		 }
		 //查询客户信息
		 LambdaQueryWrapper<CustomerInfo> queryWrapper = new LambdaQueryWrapper<>();
		 queryWrapper.setEntity(vo);
		 CustomerInfo customerInfo = customerInfoService.getOne(queryWrapper);
		 if (customerInfo == null){
			 return Result.error("账户不存在!");
		 }
         String attentionId = JwtUtil.getUserNameByToken(request);

//		 //根据token或用户openID
//		 String openId = JwtUtil.getUserNameByToken(request);
//
//		 //查询微信用户信息
//		 LambdaQueryWrapper<AttentionInfo> attentionInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
//		 attentionInfoLambdaQueryWrapper.eq(AttentionInfo::getOpenid, openId);
//		 AttentionInfo attentionInfo = attentionInfoService.getOne(attentionInfoLambdaQueryWrapper);
//		 if (attentionInfo == null){
//			 return Result.error("用户信息不存在!");
//		 }

		 //根据用户微信信息和客户信息查询是否绑定
		 LambdaQueryWrapper<CustomerAttention> customerAttentionLambdaQueryWrapper = new LambdaQueryWrapper<>();
		 customerAttentionLambdaQueryWrapper.eq(CustomerAttention::getAttentionId, attentionId);
		 customerAttentionLambdaQueryWrapper.eq(CustomerAttention::getCustomerId, customerInfo.getId());
		 CustomerAttention customerAttention = customerAttentionService.getOne(customerAttentionLambdaQueryWrapper);
		 if (customerAttention != null){
			 return Result.error("已绑定过，绑定失败!");
		 }
		 customerAttention = new CustomerAttention();
		 customerAttention.setAttentionId(attentionId);
		 customerAttention.setCustomerId(customerInfo.getId());
		 customerAttentionService.save(customerAttention);
		 return Result.ok("绑定成功！");
	 }
	
	/**
	 * 分页列表查询
	 *
	 * @param customerAttention
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
//	@AutoLog(value = "zcgl_customer_attention-分页列表查询")
	@CheckLogin
	@ApiOperation(value="微信客户关系信息-分页列表查询", notes="微信客户关系信息-分页列表查询")
	@GetMapping(value = "/queryBindingAcount")
	public Result<?> queryBindingAcount(CustomerAttention customerAttention,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

        String attentionId = JwtUtil.getUserNameByToken(req);
        customerAttention.setAttentionId(attentionId);
		QueryWrapper<CustomerAttention> queryWrapper = QueryGenerator.initQueryWrapper(customerAttention, req.getParameterMap());
		Page<CustomerAttention> page = new Page<CustomerAttention>(pageNo, pageSize);
		IPage<CustomerAttention> pageList = customerAttentionService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	/**
	 *  取消绑定
	 *
	 * @param ids
	 * @return
	 */
//	@AutoLog(value = "zcgl_customer_attention-批量删除")
	@ApiOperation(value="微信客户关系信息-取消绑定", notes="微信客户关系信息-取消绑定")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.customerAttentionService.removeByIds(Arrays.asList(ids.split(CommonConstant.DEFAULT_SEPARATOR)));
		return Result.ok("取消绑定成功!");
	}

//	/**
//	 * 分页列表查询
//	 *
//	 * @param customerAttention
//	 * @param pageNo
//	 * @param pageSize
//	 * @param req
//	 * @return
//	 */
//	@AutoLog(value = "zcgl_customer_attention-分页列表查询")
//	@ApiOperation(value="zcgl_customer_attention-分页列表查询", notes="zcgl_customer_attention-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<?> queryPageList(CustomerAttention customerAttention,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//		QueryWrapper<CustomerAttention> queryWrapper = QueryGenerator.initQueryWrapper(customerAttention, req.getParameterMap());
//		Page<CustomerAttention> page = new Page<CustomerAttention>(pageNo, pageSize);
//		IPage<CustomerAttention> pageList = customerAttentionService.page(page, queryWrapper);
//		return Result.ok(pageList);
//	}
//
//	/**
//	 *   添加
//	 *
//	 * @param customerAttention
//	 * @return
//	 */
//	@AutoLog(value = "zcgl_customer_attention-添加")
//	@ApiOperation(value="zcgl_customer_attention-添加", notes="zcgl_customer_attention-添加")
//	@PostMapping(value = "/add")
//	public Result<?> add(@RequestBody CustomerAttention customerAttention) {
//		customerAttentionService.save(customerAttention);
//		return Result.ok("添加成功！");
//	}
//
//	/**
//	 *  编辑
//	 *
//	 * @param customerAttention
//	 * @return
//	 */
//	@AutoLog(value = "zcgl_customer_attention-编辑")
//	@ApiOperation(value="zcgl_customer_attention-编辑", notes="zcgl_customer_attention-编辑")
//	@PutMapping(value = "/edit")
//	public Result<?> edit(@RequestBody CustomerAttention customerAttention) {
//		customerAttentionService.updateById(customerAttention);
//		return Result.ok("编辑成功!");
//	}
//
//	/**
//	 *   通过id删除
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "zcgl_customer_attention-通过id删除")
//	@ApiOperation(value="zcgl_customer_attention-通过id删除", notes="zcgl_customer_attention-通过id删除")
//	@DeleteMapping(value = "/delete")
//	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
//		customerAttentionService.removeById(id);
//		return Result.ok("删除成功!");
//	}
//
//	/**
//	 * 通过id查询
//	 *
//	 * @param id
//	 * @return
//	 */
//	@AutoLog(value = "zcgl_customer_attention-通过id查询")
//	@ApiOperation(value="zcgl_customer_attention-通过id查询", notes="zcgl_customer_attention-通过id查询")
//	@GetMapping(value = "/queryById")
//	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
//		CustomerAttention customerAttention = customerAttentionService.getById(id);
//		if(customerAttention==null) {
//			return Result.error("未找到对应数据");
//		}
//		return Result.ok(customerAttention);
//	}
//
//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param customerAttention
//    */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, CustomerAttention customerAttention) {
//        return super.exportXls(request, customerAttention, CustomerAttention.class, "zcgl_customer_attention");
//    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, CustomerAttention.class);
//    }

}
